Comments (10)
The produced library.js has been processed by the webpack.
I guess the import and export in the produced library.js in browser environment is not the same as the original ES6 import and export.
Import the source file should be OK.
tested run.js:
import {Cat, Dog} from './src/index.js';
const cat = new Cat();
const dog = new Dog();
from webpack-library-starter.
@alvisisme Yes, importing from the source would work. But what if I want someone else to use the bundled version as an ES6 module?
from webpack-library-starter.
I tried these steps.
- bundle the lib first (library.js / library.min.js)
- append the following code at the end of these two files
...// the bundled code
export default library;
// the end of file
- update run.js
import lib from './library.js';
// import {Cat, Dog} from './library.js';
const cat = new lib.Cat();
const dog = new lib.Dog();
console.log(cat);
Maybe not a good way, but works(in the given html page).
from webpack-library-starter.
That didn't work either.
End of the bundled file:
....
exports.default = Dog;
module.exports = exports['default'];
/***/ })
/******/ ]);
});
//# sourceMappingURL=library.js.map
export default library;
Causes: Cannot set property 'library' of undefined
Tried a few other export default export default root['library']
and such, nothing worked.
from webpack-library-starter.
@kfirm Your module only exports a single default object in which case is Dog
from
exports.default = Dog;
module.exports = exports['default'];
If you plan on using Cat
and Dog
then you should export those modules as non default exports
exports['Dog'] = Dog;
exports['Cat'] = Cat;
module.exports = exports;
EDIT:
In the above case webpack creates a modules map which is what is being loaded using the provided __webpack_require__
function.
Try doing this in your run.js
import * as library from './lib/library.js'
const dog = new library.Dog()
const cat = new library.Cat()
from webpack-library-starter.
I'm trying to version a fork of this library in a private Git repo and import it via npm. My goal is to have shared code between a NextJS frontend and an ExpressJS backend. Here's what I've done:
- Cloned this project
- Renamed the project in
package.json
fromwebpack-library-starter
tomy-lib
- Replaced occurrences of
webpack-library-starter
tomy-lib
intest/index.spec.js
- Delete all files in
lib
directory - Run
npm run build
- Pushed to a private Git repo
Now in one of my other projects (based on this boilerplate), I've added the module via:
npm install --save git+ssh://[email protected]:group/my-lib.git
There's a node_modules/my-lib
directory after this command. Now in a file of said project I add:
import {Cat} from 'my-lib';
And the project fails to build with this error:
Error: Cannot find module 'my-lib'
Any ideas where I've gone wrong? Any help is greatly appreciated!
For What It's Worth, I also tried adding this project directly (just to eliminate other potential errors from above):
npm install --save git+ssh://[email protected]:krasimir/webpack-library-starter.git
import {Cat} from 'webpack-library-starter';
And a similar error when I try to build:
Error: Cannot find module 'webpack-library-starter'
from webpack-library-starter.
@quickshiftin can you please check what's behind the main
field in my-lib/package.json
. I just noticed that here it is a wrong file. I updated this repo but you may need to edit yours too.
from webpack-library-starter.
Ok, so I'm past that last issue - I've changed the main
field in package.json
to lib/my-lib.js
. Now the error has become: ReferenceError: window is not defined
@krasimir I'm also getting the same result when I try using this project directly after your latest commit.
Line 10 of lib/webpack-library-starter.js
from webpack-library-starter.
Hey @krasimir
So I started looking around and found this Webpack issue Can't create UMD build which can be required by Node, which looks like what your library is suffering from.
I took this advice from that thread, adding this in webpack.config.js
Setting globalObject: 'typeof self !== 'undefined' ? self : this' produces exactly the same UMD header as in WebPack 3
Now your library is working from node and the browser. I'm not sure how it's working through node for other folks as it is though.
from webpack-library-starter.
The library has been updated to the latest webpack babel + typescript support. I'm not sure that the above problem exists now @quickshiftin. I just tested the following and it works:
const lib = require("./webpack-library-starter");
const d = new lib.Dog();
console.log(d.name);
the output is:
Hello world from the bundle!
Dog
My test file https://github.com/krasimir/webpack-library-starter/blob/master/lib/index.js which I run with node ./lib/index.js
.
@kfirm maybe I'm mistaken but you misused the library. The idea is to publish and get your functionality from the bundled files in the lib
folder. Not from the files under the src
directory.
Closing this issue. Please ping me if there is still problems with the latest version.
from webpack-library-starter.
Related Issues (20)
- Do you think to add Prettier to the project in the future? HOT 1
- about babel plugin. HOT 1
- Yarn: unmet peer dependency HOT 1
- Include a simple demo/docs page? HOT 4
- Webpack 4 incompatible HOT 3
- Intellisense doesnโt work after transpiled HOT 2
- Webpack4, babel7 HOT 2
- Consider making it a Yeoman|whatever-generator generator ? HOT 1
- How to use RequireJS client-side to load the Cat & Dog modules HOT 1
- Launch dev server for index.html consuming the dev library HOT 2
- Use Source Maps in Mocha Tests HOT 1
- transpiling to ES2015? Have a potential solution. HOT 1
- Doesn't support ...spread operator?
- Transpile with statics HOT 1
- How to use dynamic import HOT 1
- Help with browser HOT 1
- webpack cli error on build
- Conceptual doubt if using this project applies to my problem HOT 2
- Module build failed: Error: Cannot find module 'babel-helper-builder-binary-assignment-operator-visitor'
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. ๐๐๐
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google โค๏ธ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from webpack-library-starter.