Skip to content
This repository was archived by the owner on Aug 16, 2022. It is now read-only.

Commit 14c6a3d

Browse files
authored
feat: Use global variables as style source (#64)
* feat: Allow global variables in style source * docs: Assemble accpets global variables as style source
1 parent 28a8ebe commit 14c6a3d

File tree

7 files changed

+47
-23
lines changed

7 files changed

+47
-23
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,3 +106,5 @@ The `assemble` method is an example implementation for how to combine various pa
106106
* Directly in-lined (default)
107107
* Using a global function (normalizer: 'myComponentNormalizer')
108108
* Using an import ({ normalizer: '~my-component-normalizer' })
109+
110+
The `assemble` method also accepts global variable name in `source`, `map` and `module` of styles.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
"prepare": "yarn build",
2424
"test": "jest",
2525
"pretest": "yarn build",
26-
"changelog": "conventional-changelog -p angular -i CHANGELOG.md -s -r 1",
26+
"changelog": "conventional-changelog -p angular -r 2 -i CHANGELOG.md -s",
2727
"version": "npm run changelog && git add CHANGELOG.md"
2828
},
2929
"homepage": "https://github.com/vuejs/vue-component-compiler#readme",

src/assembler.ts

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ export function assembleFromSource(
7171
? `import ${name} from '${value.substr(1)}'`
7272
: `const ${name} = ${value}`
7373
const o = e
74+
const IDENTIFIER = /^[a-z0-9]+$/i
7475

7576
// language=JavaScript
7677
const inlineCreateInjector = `function __vue_create_injector__() {
@@ -293,18 +294,19 @@ const __vue_template__ = typeof __vue_render__ !== 'undefined'
293294
const __vue_inject_styles__ = ${hasStyle} ? function (inject) {
294295
if (!inject) return
295296
${styles.map(
296-
(style, index) =>
297-
`inject("${scopeId + '_' + index}", ${o({
298-
source: style.source,
299-
media: style.media,
300-
map: compiler.template.isProduction ? undefined : style.map
301-
})})` +
297+
(style, index) => {
298+
const source = IDENTIFIER.test(style.source) ? style.source : e(style.source)
299+
const map = !compiler.template.isProduction
300+
? (typeof style.map === 'string' && IDENTIFIER.test(style.map) ? style.map : o(style.map))
301+
: undefined
302+
const tokens = typeof style.module === 'string' && IDENTIFIER.test(style.module) ? style.module : o(style.module)
303+
304+
return `inject("${scopeId + '_' + index}", { source: ${source}, map: ${map}, media: ${e(style.media)} })` +
302305
'\n' +
303306
(style.moduleName
304-
? `Object.defineProperty(this, "${style.moduleName}", { value: ${o(
305-
style.module || {}
306-
)} })` + '\n'
307+
? `Object.defineProperty(this, "${style.moduleName}", { value: ${tokens} })` + '\n'
307308
: '')
309+
}
308310
)}
309311
} : undefined
310312
/* scoped */

src/postcss-clean.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import postcss = require('postcss')
1+
import * as postcss from 'postcss'
22
import CleanCSS = require('clean-css')
33

44
export default postcss.plugin('clean', options => {

test/assemble.spec.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import {assembleFromSource, createDefaultCompiler} from "../src"
2+
3+
const compiler = createDefaultCompiler()
4+
it('should inline style source', () => {
5+
const result = assembleFromSource(compiler, {}, {
6+
filename: 'foo.vue', script: {source: ''}, template: {source: ''}, scopeId: 'foo',
7+
styles: [{source: '.foo_0{ color: red }', module: { foo: 'foo_0' }, moduleName: '$style', map: {} }]
8+
})
9+
10+
expect(result.code).toEqual(expect.stringContaining('source: ".foo_0{ color: red }"'))
11+
expect(result.code).toEqual(expect.stringContaining('map: {}'))
12+
expect(result.code).toEqual(expect.stringContaining('value: {"foo":"foo_0"}'))
13+
})
14+
15+
it('should use global in style source', () => {
16+
const result = assembleFromSource(compiler, {}, {
17+
filename: 'foo.vue', script: {source: ''}, template: {source: ''}, scopeId: 'foo',
18+
styles: [{source: 'fooStyle', module: 'fooModule', moduleName: '$style', map: 'fooMap'}]
19+
})
20+
21+
expect(result.code).toEqual(expect.stringContaining('source: fooStyle'))
22+
expect(result.code).toEqual(expect.stringContaining('map: fooMap'))
23+
expect(result.code).toEqual(expect.stringContaining('value: fooModule'))
24+
})

test/setup/jest-helper.js

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ const path = require('path')
55
const { createDefaultCompiler, assemble } = require('../..')
66

77
const compiler = createDefaultCompiler()
8-
function compile (filename, source) {
8+
function compile(filename, source) {
99
return assemble(
1010
compiler,
1111
filename,
@@ -16,12 +16,14 @@ function compile (filename, source) {
1616
const isSourceChanged =
1717
fs.readFileSync(path.resolve(__dirname, '../../dist/compiler.js')) +
1818
':' +
19-
fs.readFileSync(path.resolve(__dirname, '../../dist/assembler.js'))
20-
':' +
19+
fs.readFileSync(path.resolve(__dirname, '../../dist/assembler.js')) +
20+
':' +
21+
fs.readFileSync(path.resolve(__dirname, '../../dist/postcss-clean.js')) +
22+
':' +
2123
fs.readFileSync(path.resolve(__dirname, '../../package.json'))
2224

2325
module.exports = {
24-
process (code, path) {
26+
process(code, path) {
2527
if (path.endsWith('.vue')) {
2628
code = compile(path, code)
2729
} else if (path.endsWith('.png')) {
@@ -31,10 +33,10 @@ module.exports = {
3133
)}"`
3234
}
3335
return babel.transform(code, {
34-
presets: [['env', { targets: { node: 'current' }}]]
36+
presets: [['env', { targets: { node: 'current' } }]]
3537
}).code
3638
},
37-
getCacheKey (fileData, filename, configString) {
39+
getCacheKey(fileData, filename, configString) {
3840
return crypto
3941
.createHash('md5')
4042
.update(

test/simple.spec.ts

Lines changed: 0 additions & 6 deletions
This file was deleted.

0 commit comments

Comments
 (0)