In my project
package.json
(listed only babel
related packages):
"@babel/core": "7.0.0-beta.37",
"@babel/plugin-syntax-dynamic-import": "7.0.0-beta.37",
"@babel/register": "7.0.0-beta.37",
"babel-eslint": "https://github.com/kesne/babel-eslint.git#patch-1",
"babel-helper-annotate-as-pure": "^7.0.0-beta.3",
"babel-loader": "^8.0.0-beta.0",
"babel-minify-webpack-plugin": "^0.2.0",
"babel-plugin-istanbul": "^4.1.5",
"babel-plugin-transform-class-properties": "^7.0.0-beta.3",
"babel-plugin-transform-decorators": "^7.0.0-beta.3",
"babel-plugin-transform-es2015-modules-commonjs": "^7.0.0-beta.3",
"babel-plugin-transform-es2015-template-literals": "^7.0.0-beta.3",
"babel-preset-typescript": "^7.0.0-alpha.19",
"webpack": "^3.5.5", "webpack-dev-server": "^2.7.1"
npm package "ui/base" is written in ES 6
.
And I am trying to import it on a page like
import '@ui/base';
.
package.json
of "ui/base".
"babel-core": "^6.26.0",
"babel-eslint": "^8.0.3",
"babel-loader": "^7.1.2",
"babel-plugin-transform-class-properties": "^6.24.1",
"babel-plugin-transform-decorators-legacy": "^1.3.4",
"babel-plugin-transform-es2015-modules-commonjs": "^6.26.0",
"babel-polyfill": "^6.26.0",
"babili-webpack-plugin": "^0.1.2",
"webpack-dev-server": "^2.7.1",
"webpack-node-externals": "^1.6.0"
The built version of a package is in ui/base/target/package/@ui/base/0/Main.js
Now, during the build process of my project, I am getting an error saying
ERROR in ./node_modules/@ui/base/src/components/accordion/Accordion.js
Module parse failed: Unexpected character '@' (18:0)
You may need an appropriate loader to handle this file type.
| import style from './accordion.css';
|
| @Define(`ui-base-v${__VERSION__}-accordion`, {
| style,
| })
The error is being thrown from the source of components. The built version of a package is not being taken in a build process of a project.
I am using following rules in webpack
to build the project.
// Resolve imports to Typescript too without needing .ts
module.exports.resolve = {
extensions: ['.js', '.ts'],
};
module.exports.rules = [
{
test: /\.(js|ts)$/,
exclude: /node_modules/,
use: [{
loader: 'babel-loader',
}],
},
{
test: /\.html$/, // plugin to import HTML as a string
use: [{
loader: 'raw-loader',
options: {
exportAsEs6Default: true,
},
}],
},
{
test: /\.(less|css)$/,
use: [
{
loader: 'css-to-string-loader', // creates style nodes from JS strings
},
{
loader: 'css-loader', // translates CSS into CommonJS,
options: { url: false },
},
{
loader: 'less-loader', // compiles Less to CSS
}],
},
];
Below is the .babelrc
in a project.
function istanbulHacks() {
return {
inherits: require("babel-plugin-istanbul").default,
};
}
const config = {
plugins: [
"@babel/plugin-syntax-dynamic-import",
"transform-decorators",
["transform-class-properties", { "loose": true }],
"transform-es2015-modules-commonjs",
["transform-es2015-template-literals", { "loose": true }],
],
presets: [
"typescript"
],
};
if (process.env.BABEL_ENV === "test") {
config.auxiliaryCommentBefore = "istanbul ignore next";
config.plugins.push(istanbulHacks);
}
module.exports = config;
Everything works fine without using the package written in ES6.
UPDATE
If I change "main" in package.json
of @ui/base
to
"main": "./target/package/@eui/base/0/Main.js",
then it works. Actually, "main" is point to "./src/index.js".
I am confused here as well. Why does "main" not point to the build version of a package ?
How to resolve this issue ? Is there any version incompatibility with babel or webpack ? Why I am not getting built version of npm package written in ES6 ?