Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#381: Added options styleInjectRelativePath to allow module import for ESM libraries #395

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -328,6 +328,24 @@ Type: `id => void`

A function to be invoked when an import for CSS file is detected.

### styleInjectRelativePath

Type: `boolean`<br/>
Default: true

The built file will contain a relative path to the style-inject module:
```js
import styleInject from '../node_modules/style-inject/dist/style-inject.es.js';`
```
This is fine if your project is the final consumer of this library.

If you are creating an ESM library to be used by other projects, then you will want to specify a module path
```js
styleInjectRelativePath: false

import styleInject from 'style-inject';
```

## License

MIT &copy; [EGOIST](https://github.com/egoist)
2 changes: 2 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ export default (options = {}) => {
namedExports: options.namedExports,
/** Automatically CSS modules for .module.xxx files */
autoModules: options.autoModules,
/** Relative style-inject import path */
styleInjectRelativePath: inferOption(options.styleInjectRelativePath, true),
/** Options for cssnano */
minimize: inferOption(options.minimize, false),
/** Postcss config file */
Expand Down
6 changes: 4 additions & 2 deletions src/postcss-loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@ import { identifier } from 'safe-identifier'
import humanlizePath from './utils/humanlize-path'
import normalizePath from './utils/normalize-path'

const styleInjectPath = require
const styleInjectRelativePath = require
.resolve('style-inject/dist/style-inject.es')
.replace(/[\\/]+/g, '/')
const styleInjectModuleName = 'style-inject'

function loadConfig(id, { ctx: configOptions, path: configPath }) {
const handleError = err => {
Expand Down Expand Up @@ -70,6 +71,7 @@ export default {
]
const shouldExtract = options.extract
const shouldInject = options.inject
const styleInject = options.styleInjectRelativePath ? styleInjectRelativePath : styleInjectModuleName

const modulesExported = {}
const autoModules = options.autoModules !== false && options.onlyModules !== true
Expand Down Expand Up @@ -209,7 +211,7 @@ export default {

if (!shouldExtract && shouldInject) {
output += typeof options.inject === 'function' ? options.inject(cssVariableName, this.id) : '\n' +
`import styleInject from '${styleInjectPath}';\n` +
`import styleInject from '${styleInject}';\n` +
`styleInject(${cssVariableName}${Object.keys(options.inject).length > 0 ?
`,${JSON.stringify(options.inject)}` :
''
Expand Down
83 changes: 83 additions & 0 deletions test/__snapshots__/index.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -1233,3 +1233,86 @@ styleInject(css_248z);
console.log(css_248z$5, css_248z$4);
"
`;

exports[`styleInject module-import: js code 1`] = `
"'use strict';

var styleInject = require('style-inject');

function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }

var styleInject__default = /*#__PURE__*/_interopDefaultLegacy(styleInject);

var css_248z$5 = \\"body {\\\\n color: red;\\\\n}\\\\n\\";
styleInject__default['default'](css_248z$5);

var css_248z$4 = \\".bar {\\\\n color: red;\\\\n}\\\\n\\";
styleInject__default['default'](css_248z$4);

var css_248z$3 = \\"body {\\\\n color: #f00;\\\\n background: #f00;\\\\n}\\\\n\\";
styleInject__default['default'](css_248z$3);

var css_248z$2 = \\"#sidebar {\\\\n width: 30%;\\\\n background-color: #faa; }\\\\n\\";
styleInject__default['default'](css_248z$2);

var css_248z$1 = \\"#header {\\\\n color: #6c94be;\\\\n}\\\\n\\";
styleInject__default['default'](css_248z$1);

var css_248z = \\".pcss {\\\\n color: red;\\\\n}\\\\n\\";
styleInject__default['default'](css_248z);

console.log(css_248z$5, css_248z$4);
"
`;

exports[`styleInject relative-import: js code 1`] = `
"'use strict';

function styleInject(css, ref) {
if ( ref === void 0 ) ref = {};
var insertAt = ref.insertAt;

if (!css || typeof document === 'undefined') { return; }

var head = document.head || document.getElementsByTagName('head')[0];
var style = document.createElement('style');
style.type = 'text/css';

if (insertAt === 'top') {
if (head.firstChild) {
head.insertBefore(style, head.firstChild);
} else {
head.appendChild(style);
}
} else {
head.appendChild(style);
}

if (style.styleSheet) {
style.styleSheet.cssText = css;
} else {
style.appendChild(document.createTextNode(css));
}
}

var css_248z$5 = \\"body {\\\\n color: red;\\\\n}\\\\n\\";
styleInject(css_248z$5);

var css_248z$4 = \\".bar {\\\\n color: red;\\\\n}\\\\n\\";
styleInject(css_248z$4);

var css_248z$3 = \\"body {\\\\n color: #f00;\\\\n background: #f00;\\\\n}\\\\n\\";
styleInject(css_248z$3);

var css_248z$2 = \\"#sidebar {\\\\n width: 30%;\\\\n background-color: #faa; }\\\\n\\";
styleInject(css_248z$2);

var css_248z$1 = \\"#header {\\\\n color: #6c94be;\\\\n}\\\\n\\";
styleInject(css_248z$1);

var css_248z = \\".pcss {\\\\n color: red;\\\\n}\\\\n\\";
styleInject(css_248z);

console.log(css_248z$5, css_248z$4);
"
`;
17 changes: 17 additions & 0 deletions test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,23 @@ snapshotMany('minimize', [
}
])

snapshotMany('styleInject', [
{
title: 'relative-import',
input: 'simple/index.js',
options: {
styleInjectRelativePath: true
}
},
{
title: 'module-import',
input: 'simple/index.js',
options: {
styleInjectRelativePath: false
}
}
])

snapshotMany('modules', [
{
title: 'inject',
Expand Down